<?php
require_once BASE_PATH.'application/libraries/excel/PHPExcel/IOFactory.php';
require_once BASE_PATH.'application/libraries/excel/PHPExcel.php';

class excelDAO extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function load($data = array()){
        
        $data = overwrite_array(array(
                                      "tipo"  =>"Excel2007",
                                      "path"   => "",
                                      "index"  => 0
                                      ),$data);
        
        $excel = PHPExcel_IOFactory::createReader($data["tipo"]);
        $excel = $excel->load($data["path"]); // Empty Sheet
        $excel->setActiveSheetIndex($data["index"]);
        return $excel;
    }
    
    function save($data = array())
    {
        $data = overwrite_array(array(
                                      "tipo"  =>"Excel2007",
                                      "path"   => "",
                                      "excel"  => ""
                                      ),$data);
        $objWriter = PHPExcel_IOFactory::createWriter($data["excel"], $data["tipo"]);
        $objWriter->save($data["path"]);
    }
    
    function copySheet($data = array())
    {
        $objWorkSheet1 = clone $data["sheet"];
        $objWorkSheet1->setTitle($data["title"]);
        $data["excel"]->addSheet($objWorkSheet1);
        return $objWorkSheet1;
    }
    
    function insertImage($data = array()){
        
        $data = overwrite_array(array(
                                      "sheet"        => "",
                                      "name"         => "",
                                      "descripcion"  => "",
                                      "path"         => "",
                                      "celd"         => ""
                                      ),$data);
        
        $objDrawing = new PHPExcel_Worksheet_Drawing();
        $objDrawing->setWorksheet($data["sheet"]);
        $objDrawing->setName($data["name"]);
        $objDrawing->setDescription($data["descripcion"]);
        $objDrawing->setPath($data["path"]);
        $objDrawing->setCoordinates($data["celd"]);
        $objDrawing->setOffsetX(0);
        $objDrawing->setOffsetY(0);      
    }
}
?>